<?php
/**
 * User: aka
 * Time: 1:57
 */

namespace app\tenno\controller;

use app\common\CommentEmail;
use app\validate\plug\SendEmail;
use app\validate\users\SendEmailComment;
use think\Request;

class Email
{
    private $emailPlugIn;
    private $commentEmailPlugIn;
    private $userInfo;

    public function __construct()
    {
        $this->emailPlugIn = new \app\common\Email();
        $this->userInfo = session('user_info');
        $this->commentEmailPlugIn = new CommentEmail();
    }

    public function __destruct()
    {
        unset(
            $this->emailPlugIn,
            $this->userInfo,
            $this->commentEmailPlugIn
        );
    }

    public function sendEmail(Request $request)
    {
        $validate = new SendEmail();
        if (!$validate->check($request->param())) {
            return json($validate->getError());
        }else{
            $emailAddress = $request->param('email_address');
            return $this->emailPlugIn->setEmailConfigAndSendEmail($emailAddress);
        }
    }

    public function sendForgetPasswordEmail(Request $request)
    {
        $validate = new SendEmail();
        if (!$validate->check($request->param())) {
            return json($validate->getError());
        }else{
            $emailAddress = $request->param('email_address');
            return $this->emailPlugIn->setEmailConfigAndSendForgetPasswordEmail($emailAddress);
        }
    }

    public function sendUserCommentToEmail(Request $request)
    {
        $validate = new SendEmailComment();
        if (!$validate->check($request->param())) {
            return json($validate->getError());
        }else{
            $receiveUserId = base64_decode($request->param('receive_user_id'));
            $commentContent = $request->param('comment_content');
            $sendUserName = $this->userInfo['game_id'];
            $sendUserAccount = $this->userInfo['email'];
            $receiveUserInfo = \app\tenno\model\Users::userInfo($receiveUserId)->find();
            $this->commentEmailPlugIn->setEmailContent($sendUserName, $commentContent);
            $this->commentEmailPlugIn->setSendUserAccount($sendUserAccount);
            $this->commentEmailPlugIn->setReceiveAddress($receiveUserInfo['email']);
            return $this->commentEmailPlugIn->send();
        }
    }
}